import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum {
    public static List<List<Integer>> code(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ret = new ArrayList<>();

        for (int i = 0; nums[i] <= 0;){
            int target = -nums[i];
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right){
                if (nums[left] + nums[right] > target){
                    right--;
                }else if (nums[left] + nums[right] < target){
                    left++;
                }else{
                    ret.add(Arrays.asList(nums[left], nums[right], nums[i]));
                    left++;
                    right--;

                    while (left < right && nums[left] == nums[left - 1]){
                        left++;
                    }

                    while (left < right && nums[right] == nums[right + 1]){
                        right--;
                    }
                }
            }

            i++;
            while (i < nums.length && nums[i] == nums[i - 1]){
                i++;
            }
        }

        return ret;
    }
    public static void main(String[] args) {
        ThreeSum.code(new int[]{0, 0, 0});
    }
}
